package com.ssm.controller;

import com.ssm.entity.TbSpecification;
import com.ssm.service.TbSpecificationService;
import com.ssm.util.PageResult;
import com.ssm.util.R;
import com.ssm.vo.TbSpecificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tbspecification")
@CrossOrigin
public class TbSpecificationController {
    @Autowired
    TbSpecificationService tbSpecificationService;

    // 查询所有的规格
    @GetMapping("/list")
    public List<TbSpecification> list() {
        return tbSpecificationService.findAll();
    }

    //根据页码获取规格信息
    @GetMapping("/findByPage")
    public PageResult<TbSpecification> findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize) {
        return tbSpecificationService.findByPage(page, pageSize);
    }

    // 分页查找规格，其中page,pageSize为必须参数，规格对象为可选参数
    @PostMapping("/search")
    public PageResult<TbSpecification> search(@RequestParam("page") Integer page,
                                              @RequestParam("pageSize") Integer pageSize,
                                              @RequestBody(required = false) TbSpecificationVO tbSpecificationVO) {
        return tbSpecificationService.search(page, pageSize, tbSpecificationVO);
    }

    // 保存规格
    @PostMapping("/save")
    public R save(@RequestBody TbSpecification tbSpecification) {
        return tbSpecificationService.save(tbSpecification) ? R.ok().message("规格添加成功！") : R.error().message("规格添加失败！");
    }
}
